#!/bin/bash

window_state() {
	source "$CONFIG_DIR/colors.sh"
	source "$CONFIG_DIR/icons.sh"

	WINDOW=$(yabai -m query --windows --window)
	STACK_INDEX=$(echo "$WINDOW" | jq '.["stack-index"]')

	COLOR=$BAR_BORDER_COLOR
	ICON=""

	if [ "$(echo "$WINDOW" | jq '.["is-floating"]')" = "true" ]; then
		ICON+=$YABAI_FLOAT
		COLOR=$RED
	elif [ "$(echo "$WINDOW" | jq '.["has-fullscreen-zoom"]')" = "true" ]; then
		ICON+=$YABAI_FULLSCREEN_ZOOM
		COLOR=$GREEN
	elif [ "$(echo "$WINDOW" | jq '.["has-parent-zoom"]')" = "true" ]; then
		ICON+=$YABAI_PARENT_ZOOM
		COLOR=$BLUE
	elif [[ $STACK_INDEX -gt 0 ]]; then
		LAST_STACK_INDEX=$(yabai -m query --windows --window stack.last | jq '.["stack-index"]')
		ICON+=$YABAI_STACK
		LABEL="$(printf "[%s/%s]" "$STACK_INDEX" "$LAST_STACK_INDEX")"
		COLOR=$MAGENTA
	fi

	args=(--bar border_color=$COLOR --animate sin 10 --set $NAME icon.color=$COLOR)

	[ -z "$LABEL" ] && args+=(label.width=0) ||
		args+=(label="$LABEL" label.width=40)

	[ -z "$ICON" ] && args+=(icon.width=0) ||
		args+=(icon="$ICON" icon.width=30)

	sketchybar -m "${args[@]}"
}

mouse_clicked() {
	yabai -m window --toggle float
	window_state
}

case "$SENDER" in
"mouse.clicked")
	mouse_clicked
	;;
"window_focus")
	window_state
	;;
esac
